package com.virtium.vpc.dao.hibernate;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.virtium.vpc.dao.VpcProductstatusDAO;
import com.virtium.vpc.hibernate.model.VpcProductstatus;

/**
 * A data access object (DAO) providing persistence and search support for
 * VpcProductstatus entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see aa.VpcProductstatus
 * @author NguyenTuanAnh AnhNguyen@viritum.com
 */

public class VpcProductstatusDAOHibernate extends BaseHibernateDAO implements
		VpcProductstatusDAO {
	private static final Log log = LogFactory
			.getLog(VpcProductstatusDAOHibernate.class);
	// property constants
	public static final String TEXT = "text";

	public void save(VpcProductstatus transientInstance) {
		log.debug("saving VpcProductstatus instance");
		try {
			Transaction tx = getSession().beginTransaction();
			hsession.save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public void delete(VpcProductstatus persistentInstance) {
		log.debug("deleting VpcProductstatus instance");
		try {
			Transaction tx = getSession().beginTransaction();
			hsession.delete(persistentInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public VpcProductstatus findById(java.lang.Integer id) {
		log.debug("getting VpcProductstatus instance with id: " + id);
		try {
			VpcProductstatus instance = (VpcProductstatus) getSession().get(
					"com.virtium.vpc.hibernate.model.VpcProductstatus", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByExample(VpcProductstatus instance) {
		log.debug("finding VpcProductstatus instance by example");
		try {
			List results = getSession().createCriteria(
					"com.virtium.vpc.hibernate.model.VpcProductstatus").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding VpcProductstatus instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from VpcProductstatus as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByText(Object text) {
		return findByProperty(TEXT, text);
	}

	public List findAll() {
		log.debug("finding all VpcProductstatus instances");
		try {
			String queryString = "from VpcProductstatus";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public VpcProductstatus merge(VpcProductstatus detachedInstance) {
		log.debug("merging VpcProductstatus instance");
		try {
			VpcProductstatus result = (VpcProductstatus) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(VpcProductstatus instance) {
		log.debug("attaching dirty VpcProductstatus instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public void attachClean(VpcProductstatus instance) {
		log.debug("attaching clean VpcProductstatus instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}
}